﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CSDL_NC_07.DAO;
using System.Data.SqlClient;
using CSDL_NC_07.BUS;

namespace CSDL_NC_07.GUI.other
{
    public partial class MHThemHDTU : UserControl
    {
        private THUNGAN thuNgan = null;
        public MHThemHDTU(THUNGAN _thuNgan)
        {
            InitializeComponent();
            thuNgan = _thuNgan;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            xoaForm();
        }

        private void btnLapHD_Click(object sender, EventArgs e)
        {
            string _hsnvId = txtMaHSNV.Text;
            string _soTien = txtSoTien.Text;
            int _iHSNVId;
            double _dSoTien;
            if (_hsnvId == "")
            { 
                lblThongBao.Text = "CHƯA NHẬP MÃ HỒ SƠ NHẬP VIỆN";
                return;
            }
            
            if(_soTien == "")
            {
                lblThongBao.Text = "CHƯA NHẬP SỐ TIỀN";
                return;
            }

            try
            {
                _iHSNVId = int.Parse(_hsnvId);
                _dSoTien = double.Parse(_soTien);
            }
            catch 
            {
                lblThongBao.Text = "SAI ĐỊNH DẠNG THÔNG TIN NHẬP VÀO. VUI LÒNG KIỂM TRA LẠI.";
                return;
            }

            DataProvider dataProvider = new DataProvider();
            if (!dataProvider.IsConnect())
                dataProvider.Connect();

            List<SqlParameter> parameters = new List<SqlParameter>();
            SqlParameter _paraSoTien = new SqlParameter("@SoTien", SqlDbType.Money);
            _paraSoTien.Value = _dSoTien;
            parameters.Add(_paraSoTien);

            SqlParameter _paraThuNgan = new SqlParameter("@ThuNgan_ID", SqlDbType.Int);
            _paraThuNgan.Value = thuNgan.ThuNgan_ID;
            parameters.Add(_paraThuNgan);

            SqlParameter _paraHSNV = new SqlParameter("@HSNV_ID", SqlDbType.Int);
            _paraHSNV.Value = _iHSNVId;
            parameters.Add(_paraHSNV);

            SqlParameter _paraNgayLap = new SqlParameter("@NgayLap_HDTU", SqlDbType.DateTime);
            _paraNgayLap.Value = DateTime.Now;
            parameters.Add(_paraNgayLap);

            SqlParameter _paraOut = new SqlParameter("@Out", SqlDbType.Int) { Direction = ParameterDirection.Output };
            parameters.Add(_paraOut);

            try
            {
                dataProvider.ExcuteProcedure("sp_ThemHoaDon_TamUng", parameters.ToArray());
                if (int.Parse(_paraOut.Value.ToString()) == 1)
                {
                    MessageBox.Show("LẬP HÓA ĐƠN TẠM ỨNG THÀNH CÔNG");
                    xoaForm();
                }
                else
                    MessageBox.Show("LỖI");
            }
            catch
            {
                MessageBox.Show("LỖI");
            }
        }

        private void xoaForm()
        {
            txtCMND.Text = "";
            txtHoTen.Text = "";
            txtNgaySinh.Text = "";
            txtMaHSNV.Text = "";
            txtSoTien.Text = "";
            lblThongBao.Text = "";
        }

        private void txtSoTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != ',' && e.KeyChar != '.' && e.KeyChar != (char)46 && e.KeyChar != (char)8)
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                {
                    e.Handled = true;
                }
            }
        }

        private void txtMaHSNV_TextChanged(object sender, EventArgs e)
        {
            lblThongBao.Text = "";
            string _hsnvId = txtMaHSNV.Text;
            if (_hsnvId != "")
            {
                DataProvider dataProvider = new DataProvider();
                string sqlStr = "select hsnv.CTBA_ID from HOSO_NHAPVIEN hsnv where hsnv.HSNV_ID = " + _hsnvId;
                if (!dataProvider.IsConnect())
                    dataProvider.Connect();
                DataTable dt = dataProvider.ExcuteQueryDataTable(sqlStr);
                if (dt.Rows.Count == 1)
                {
                    sqlStr = "select top 1 ctba.BenhAn_ID from CHITIET_BENHAN ctba where ctba.CTBA_ID = " + dt.Rows[0][0].ToString();
                    dt = dataProvider.ExcuteQueryDataTable(sqlStr);
                    sqlStr = "select top 1 ba.HSBN_ID from BENHAN ba where ba.BenhAn_ID = " + dt.Rows[0][0].ToString();
                    dt = dataProvider.ExcuteQueryDataTable(sqlStr);
                    sqlStr = "select top 1 * from HOSO_BENHNHAN hsbn where hsbn.HSBN_ID = " + dt.Rows[0][0].ToString();
                    dt = dataProvider.ExcuteQueryDataTable(sqlStr);
                    txtHoTen.Text = dt.Rows[0]["HoTen"].ToString();
                    txtCMND.Text = dt.Rows[0]["SoCMND"].ToString();
                    txtNgaySinh.Text = dt.Rows[0]["NgaySinh"].ToString();
                }
                else
                {
                    txtHoTen.Text = "";
                    txtCMND.Text = "";
                    txtNgaySinh.Text = "";
                }
                dataProvider.Disconnect();
            }
            else
            {
                txtHoTen.Text = "";
                txtCMND.Text = "";
                txtNgaySinh.Text = "";
            }
        }

        private void txtMaHSNV_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != ',' && e.KeyChar != '.' && e.KeyChar != (char)46 && e.KeyChar != (char)8)
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                {
                    e.Handled = true;
                }
            }
        }

        private void MHThemHDTU_Load(object sender, EventArgs e)
        {
            lblThongBao.Text = "";
            txtMaHSNV.Focus();
        }

    }
}
